using System.Collections;
using System.Collections.Generic;
using TowerDefence;
using UnityEngine;


namespace  TowerDefence_Study
{
    public class UIManager : MonoBehaviour
    {

        private const string UIPath = "UIPrefabs/";

        private static GameObject mUIRoot;
        
        private static List<MonoBehaviour> mUIList = new List<MonoBehaviour>();

        private static void InitData()
        {
            mUIRoot=GameObject.Find("Canvas");
        }
        public static T EnterUI<T>()where T:UILayer
        {
            if(mUIRoot==null)
            {
                InitData();
            }
            
            GameObject obj=Resources.Load<GameObject>(UIPath+typeof(T).Name);
            GameObject ui=Instantiate(obj,mUIRoot.transform);
            
            ui.transform.localPosition=Vector3.zero;
            ui.transform.localScale=Vector3.one;
            ui.transform.localRotation=Quaternion.identity;
            
            RectTransform rect=ui.GetComponent<RectTransform>();
            rect.anchorMin=Vector2.zero;
            rect.anchorMax=Vector2.one;
            rect.offsetMin=Vector3.zero;
            rect.offsetMax=Vector3.zero;
            
            T t=ui.AddComponent<T>();
            t.InitNode(t.transform);
            t.OnNodeLoad();
            mUIList.Add(t);
            return t;


        }
        public static void ExitUI(MonoBehaviour mono)
        {
            mUIList.Remove(mono);
            GameObject.Destroy(mono.gameObject);
            
        }

        public static void ExitAllUI()
        {
            for (int i = 0; i < mUIList.Count; i++)
            {
                GameObject.Destroy(mUIList[i].gameObject);
            }
            mUIList.Clear();
        }
        
        
        
    }
    

}
